package com.zengqingfa.rocketmq.batch;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @fileName: BatchMessageProducer
 * @author: zengqf3
 * @date: 2021-4-2 16:21
 * @description:
 */
public class BatchMessageProducer {
    public static void main(String[] args) throws Exception {

        // 1、创建DefaultMQProducer
        DefaultMQProducer producer = new DefaultMQProducer("demo_producer_broadcasting_group");

        // 2、设置Namesrv地址
        producer.setNamesrvAddr("localhost:9876");

        // 3、开启DefaultMQProducer
        producer.start();

        // 4、创建消息Message
        // topic：主题，tags: 标签，主要用于消息过滤，keys：消息的唯一值，body：消息体
        List<Message> messages = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            Message message = new Message("TestTopic", "BATCH", "Keys_" + i,
                    ("Hello RocketMQ_" + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
            messages.add(message);
        }
        // 5、发送消息
        SendResult result = producer.send(messages);
        System.out.println(result);

        // 6、关闭DefaultMQProducer
        producer.shutdown();
    }
}
